home *** CD-ROM | disk | FTP | other *** search
- #include <Dialogs.h>
- #include <Events.h>
- #include <Files.h>
- #include <Stdlib.h>
- #include <Types.h>
- #include <Resources.h>
- #include <console.h>
- #include "ui.h"
-
- #if defined( THINK_C ) || defined ( __MWERKS__ )
- #include <stdio.h>
- #include <string.h>
- #include <strings.h>
- #include "os_mac_eventchk.h"
- #ifdef THINK_C
- #include <fcntl.h>
- #include <stat.h>
- #else
- #include <unix.h>
- #endif
- #endif
-
- #include <Gestalt.h>
-
- char *get_wd_name (short wd_refnum, char *postfix)
- {
- #if defined( THINK_C ) || defined( __MWERKS__ )
- unsigned char buf [64];
- #else
- char buf [64];
- #endif
- char *temp, *result;
- DirInfo cinfo;
- WDPBRec wd;
-
- wd.ioCompletion = NULL;
- wd.ioNamePtr = NULL;
- wd.ioVRefNum = wd_refnum;
- wd.ioWDIndex = 0;
- wd.ioWDProcID = 0;
- wd.ioWDVRefNum = 0;
- if( PBGetWDInfoSync( &wd ) != noErr ) return postfix;
- cinfo.ioDrParID = wd.ioWDDirID;
- result = malloc (strlen (postfix) + 1);
- if (result == NULL) return postfix;
- strcpy (result, postfix);
- do{
- cinfo.ioCompletion = NULL;
- cinfo.ioNamePtr = buf;
- cinfo.ioVRefNum = wd.ioWDVRefNum;
- cinfo.ioFDirIndex = -1;
- cinfo.ioDrDirID = cinfo.ioDrParID;
- if (PBGetCatInfoSync ((CInfoPBPtr) &cinfo) != noErr) return postfix;
- #ifdef THINK_C
- temp = malloc (strlen (result) + buf[0] + 2);
- #else
- p2cstr (buf);
- temp = malloc (strlen (result) + strlen ((char *)buf) + 2);
- #endif
- if (temp == NULL){
- free (result);
- return postfix;
- }
- #ifdef THINK_C
- sprintf (temp, ":%#s%s", buf, result);
- #else
- sprintf (temp, ":%s%s", buf, result);
- #endif
- free (result);
- result = temp;
- }while (cinfo.ioDrDirID != 2);
- return result + 1;
- }
-
- #ifdef __MC68K__
- #if __MC68K__
- static void die( unsigned char *txt )
- {
- InitGraf( &qd.thePort );
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs( 0L );
- InitCursor();
- ParamText( txt, "\p", "\p", "\p" );
- StopAlert( ok_alertID, 0L );
- ExitToShell();
- }
- #endif
- #endif
-
- extern int sub_main (int argc, char *argv []);
-
- extern unsigned char *console_title;
-
- extern char *image_name = 0;
-
- /* CAMLRUN 10Jan95 e
- 0 for a command line interface to load any Caml link'd file
- 1 to load image named in string resource
- */
-
- #define CAMLRUN 1
-
- #ifdef THINK_C
- extern WindowPeek cflush(FILE *fp);
- #endif
-
- static long probe_image( char *name ) // returns length or -1
- {
- long len = -1;
- int fd = open( name, O_RDONLY | O_BINARY );
- if (fd != -1)
- {
- len = lseek( fd, 0, SEEK_END );
- close( fd );
- }
- return len;
- }
-
- #define MIN_IMAGE_SIZE 1200
-
- main ()
- {
- int argc, fd;
- char **argv;
- WDPBRec wd;
- char *args[6];
- KeyMap kMap;
- long len;
- Str255 pstr;
-
- if ( GetApplLimit() > ( LMGetCurStackBase() - 40000 ) )
- { SetApplLimit ( LMGetCurStackBase() - 40000 );
- }
-
- #ifdef __MC68K__
- #if __MC68K__
- #if __MC68020__
- if ( /* NG on Sys 7.1 and others? -- 08Oct96 e
- Gestalt( gestaltNativeCPUtype, &len ) || len < gestaltCPU68020
- */
- Gestalt( gestaltProcessorType, &len ) || len < gestalt68020
- )
- { die( "\pAn M68020 or better processor is required to run this application!" );
- }
- #endif
- #if __MC68881__
- if ( Gestalt( gestaltFPUType, &len ) || len == gestaltNoFPU )
- { die( "\pAn FPU (or M64040) is required to run this application!\
- Try using the SANE version." );
- }
- #endif
- #endif
- #endif
-
- /* 10Jan95 e -- data fork is now PPC code...
- char apname[32];
- unsigned char *CurApName = LMGetCurApName();
- memcpy(apname,((char *)CurApName + 1),*((char *)CurApName));
- apname[*((char *)CurApName)] = 0;
- */
-
- #if CAMLRUN
-
- wd.ioCompletion = NULL;
- wd.ioNamePtr = NULL;
- PBHGetVolSync( &wd );
-
- // first, see if we were launched with an image document
- // to do that we need to init MacOS which is done by the console
- GetIndString(pstr, 357, 5); /* console title */
- if ( pstr[0] == 0 )
- console_title = "\pMosml Console";
- else
- console_title = pstr;
- #ifdef THINK_C
- cflush(stdin);
- #else
- puts(" ");
- fflush(stdout);
- #endif
- for (fd=0; fd<20; fd++) os_event_check(); /* let any open apple events in */
- /* arg 0 */
- len = (image_name == NULL) ? 0 : probe_image( image_name );
- if( len >= MIN_IMAGE_SIZE )
- {
- args[0] = image_name;
- }
- else
- {
- GetIndString(pstr, 357, 1); /* image name */
- p2cstr(pstr);
- args[0] = get_wd_name( wd.ioVRefNum, (char *)pstr );
- len = probe_image( args[0] );
- }
- if( len >= 0 )
- {
- GetKeys( kMap );
- // fd = ((len < 12000) || (kMap[1] & 4)) ? -1 : 0; -- option key
- fd = ((len < MIN_IMAGE_SIZE) || (kMap[1] & 32768L)) ? -1 : 0;
- } /* probe image, insure big enough, command key for command line */
- else fd = -1;
-
- /* arg 1 */
- args[1] = "-stdlib";
-
- /* arg 2 */
- GetIndString(pstr, 357, 2); /* stdlib directory name */
- p2cstr(pstr);
- args[2] = get_wd_name( wd.ioVRefNum, (char *)pstr );
-
- argc = 3;
-
- /* optional arg 3 */
- GetIndString(pstr, 357, 3); /* options, e.g., -P */
- if ( pstr[0] != 0 )
- {
- args[3] = malloc( pstr[0] );
- if( args[3] != NULL )
- { argc = 4;
- p_to_c( pstr, args[3] );
- /* optional arg 4 */
- GetIndString(pstr, 357, 4); /* options, e.g., {full,none,etc.} */
- if ( pstr[0] != 0 )
- {
- args[4] = malloc( pstr[0] );
- if( args[4] != NULL )
- { argc = 5;
- p_to_c( pstr, args[4] );
- }
- }
- }
- }
-
- #else
-
- fd = -1;
-
- #endif
-
- if ( fd != -1 )
- {
- argv = args;
- argv[argc] = NULL;
- /* moved above
- #ifdef THINK_C
- cflush(stdin);
- #else
- puts(" ");
- fflush(stdout);
- #endif
- */
- }
- else
- {
- if ( 1 /* kMap[1] & 1 */ ) // shift key
- {
- fputs(args[0],stdout);
- fputs(" ",stdout);
- fputs(args[1],stdout);
- fputs(" ",stdout);
- fputs(args[2],stdout);
- fputs(" ",stdout);
- fputs(args[3],stdout);
- fputs(" ",stdout);
- puts(args[4]);
- }
- argc = ecommand( (unsigned char ***)&argv );
- }
-
- if( argc > 0 ) // ecommand can do this now -- 13May97 e
- {
- init_timers();
- caml_main( argc, argv );
- }
- return 0;
- }
-
- // end of uio.c
-